package com.hdw.job.controller;

import com.hdw.common.base.BaseException;
import com.hdw.common.base.BaseResult;
import com.hdw.common.constant.CommonResultConstant;
import com.hdw.med.bean.vo.TableDDLColumnVO;
import com.hdw.common.vo.LayMsg;
import com.hdw.med.bean.vo.TableDDLVO;
import com.hdw.common.vo.PaginationVO;
import com.hdw.job.api.EtlTaskOdsService;
import com.hdw.job.bean.model.EtlTaskOds;
import com.hdw.med.bean.vo.TableDDLSqlVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * etlTaskOds控制器
 * Created by jianghq on 2021/11/24.
 */

@RestController
@RequestMapping("/etl/etlTaskOds/")
@Api(tags = "ETL基础配置")
@CrossOrigin(origins = "*", maxAge = 3600)
public class EtlTaskOdsController {

    @Autowired
    EtlTaskOdsService etlTaskOdsService;

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "findByPage", method = RequestMethod.GET)
    public LayMsg findByPage(Integer page, Integer limit, String filter) {
        EtlTaskOds etlTaskOds = new EtlTaskOds();
        etlTaskOds.setTbTar(filter);
        PaginationVO<EtlTaskOds> pageVO = etlTaskOdsService.findByModelWithBLOBs(etlTaskOds, page, limit);
        return LayMsg.success().addCount(pageVO.getTotal()).addData(pageVO.getEntryList());
    }

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "findUseByPage", method = RequestMethod.GET)
    public LayMsg findUseByPage(Integer page, Integer limit, String filter) {
        EtlTaskOds etlTaskOds = new EtlTaskOds();
        etlTaskOds.setTbTar(filter);
        etlTaskOds.setFgStop("0");
        PaginationVO<EtlTaskOds> pageVO = etlTaskOdsService.findPageByModel(etlTaskOds, page, limit);
        return LayMsg.success().addCount(pageVO.getTotal()).addData(pageVO.getEntryList());
    }

    @ApiOperation(value = "查询")
    @RequestMapping(value = "find", method = RequestMethod.GET)
    public LayMsg find(Integer page, Integer limit) {
        List<EtlTaskOds> pageVO = etlTaskOdsService.findByModel(new EtlTaskOds());
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value = "保存")
    @ApiImplicitParam(name = "etlTaskOds", value = "实体", paramType = "body", required = true, dataType = "etlBase")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public LayMsg save(@RequestBody EtlTaskOds etlTaskOds, HttpServletRequest request) throws BaseException {
        int num;
        if (StringUtils.isEmpty(etlTaskOds.getIdTaskOds())) {
            num = etlTaskOdsService.insertSelective(etlTaskOds);
        } else {
            num = etlTaskOdsService.updateByIdSelective(etlTaskOds);
        }
        return LayMsg.success().addData(num);
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParam(name = "idTaskOds", value = "主键", paramType = "query", required = true, dataType = "Long")
    @RequestMapping(value = "getById", method = RequestMethod.GET)
    public BaseResult getById(Long idTaskOds, HttpServletRequest request) throws BaseException {
        EtlTaskOds etlTaskOds = etlTaskOdsService.getById(idTaskOds);
        return new BaseResult(CommonResultConstant.SUCCESS, etlTaskOds);
    }

    @ApiOperation(value = "修改")
    @ApiImplicitParam(name = "etlTaskOds", value = "实体", paramType = "body", required = true, dataType = "EtlBase")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseResult update(@RequestBody EtlTaskOds etlTaskOds, HttpServletRequest request) throws BaseException {
        int num = etlTaskOdsService.updateByIdSelective(etlTaskOds);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParam(name = "idTaskOds", value = "主键", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public BaseResult delete(Long idTaskOds, HttpServletRequest request) throws BaseException {
        try {
            int num = etlTaskOdsService.deleteById(idTaskOds);
            return new BaseResult(CommonResultConstant.SUCCESS, num);
        } catch (Exception e) {
            return new BaseResult(CommonResultConstant.ERROR, "删除失败！原因：" + e.getMessage());
        }
    }

    @ApiOperation(value = "返回建表字字段列表")
    @RequestMapping(value = "gerTableColumns", method = RequestMethod.GET)
    public LayMsg gerTableColumns(Long idTaskOds) {
        if (idTaskOds == null) {
            return null;
        }
        try {
            List<TableDDLColumnVO> columnList = etlTaskOdsService.createOrModiTableColumnList(idTaskOds);
            return LayMsg.success().addCount(columnList.size()).addData(columnList);
        } catch (Exception e) {
            e.printStackTrace();
            return LayMsg.fail().addMsg(e.getMessage());
        }
    }

    @ApiOperation(value = "返回建表SQL")
    @RequestMapping(value = "gerTableDDL", method = RequestMethod.GET)
    public LayMsg createTableSQL(Long idTaskOds) {
        try {
            TableDDLSqlVO createTableDDLVO = etlTaskOdsService.createOrModiTableSQL(idTaskOds);
            return LayMsg.success().addData(createTableDDLVO);
        } catch (Exception e) {
            e.printStackTrace();
            return LayMsg.fail().addMsg(e.getMessage());
        }
    }

    /**
     * @return
     */
    @ApiOperation(value = "执行建表字段列表")
    @RequestMapping(value = "execTableDDLColumns", method = RequestMethod.POST)
    public LayMsg execTableDDLColumns(@RequestBody TableDDLVO opTable) {
        try {
            etlTaskOdsService.execTableDDLColumns(opTable);
            return LayMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return LayMsg.fail().addMsg(e.getMessage());
        }
    }

    /**
     * @param idTaskOds
     * @param execType  1.新建   2修改
     * @return
     */
    @ApiOperation(value = "执行建表")
    @RequestMapping(value = "execTableDDLSQL", method = RequestMethod.GET)
    public LayMsg execTableDDLSQL(Long idTaskOds, int execType) {
        try {
            etlTaskOdsService.execTableDDLSQL(idTaskOds, execType);
            return LayMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return LayMsg.fail().addMsg(e.getMessage());
        }
    }
}
